package com.guiji.quartz.controller;

import com.google.api.services.youtube.model.Video;
import com.guiji.common.core.domain.AjaxResult;
import com.guiji.common.core.page.TableDataInfo;
import com.guiji.quartz.service.IYoutubePopStatisticsService;
import com.guiji.quartz.vo.YoutubePopStatisticsBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotEmpty;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping
public class YoutubePopStatisticsController {

	@Autowired
	private IYoutubePopStatisticsService youtubePopStatisticsService;

	@GetMapping("/getPopStatistics")
	public AjaxResult getPopStatistics(@NotEmpty(message = "url不能为空") String url, @NotEmpty(message = "rankViewCount不能为空") int rankViewCount, String startTime) {
		List<YoutubePopStatisticsBo> results = youtubePopStatisticsService.getPopStatistics(url, rankViewCount, startTime);
		if (null == results) {
			return AjaxResult.error("网络异常或者没有符合条件的热点统计信息");
		} else {
			return AjaxResult.success(results);
		}
	}

	@GetMapping("/getVideoStatistics")
	public AjaxResult getVideoStatistics(@NotEmpty(message = "url不能为空") String url, @NotEmpty(message = "rankViewCount不能为空") int rankViewCount, String startTime) {
		List<YoutubePopStatisticsBo> results = youtubePopStatisticsService.getPopStatistics(url, rankViewCount, startTime);
		List<String> videoIds = new ArrayList<>();
		if (results != null && results.size() > 0) {
			for (YoutubePopStatisticsBo bo : results) {
				videoIds.add(bo.getVideoId());
			}
		}
		List<Video> videoInfos = null;
		if (videoIds.size() > 0) {
			videoInfos = youtubePopStatisticsService.getVideoDetailByVideoId(videoIds);
		}

		if (null == videoInfos) {
			return AjaxResult.error("网络异常或者没有符合条件的热点统计信息");
		} else {
			return AjaxResult.success(videoInfos);
		}
	}

}

